﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using System.Data;
using System.Text.RegularExpressions;

namespace BLL
{
    public enum DKForm
    {
        thietbi = 0,
        KQBaoTri,
        taikhoan,
        TBLoiMT,
        TBLoiTB,
        DeXuat
    };

    public enum taikhoan
    {
        MaTk=0,
        TenDangNhap,
        LanDangNhapCuoi,
        MayDangNhap,
        TrangThai
    };
    public enum thietbi
    {
        MaTB=0,
        LoaiTB,
        NhanHieu,
        XuatXu,
        NgayNhap,
        NgayLapRap,
        TinhTrang
    };

    public enum thongtintaikhoan
    {
        MaTK=0,
        Ho,
        Ten,
        NgaySinh,
        CMND,
        SDT,
        ChucVu
    };

    public enum ketquabaotri
    {
        MaThongBao=0,
        MaNguoiGui,
        MaTB,
        TinhTrangLoi,
        NgayBaoTri,
        KetQua,
    };

    public enum thongbaoloimt
    {
        MaLoiMT=0,
        MaLoiTB,
        TenPhanBiLoi,
        ChiTietLoi
    }

    public enum thongbaoloitb
    {
        MaLoiTB=0,
        MaNguoiGui,
        MaTB,
        LoaiTB,
        NoiDungLoi,
        NgayGui,
        GhiChu
    };
    public enum dexuat
    {
        MaDX=0,
        MaNguoiGui,
        NoiDung
    };
    
    public class ThuVien
    {
       public static void TRY_CATCH(Action act)
       {
           try
           {
               act();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
         public static void TRY_CATCH(Action act,int x)
       {
           try
           {
               act();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
       public static void TRY_CATCH(string tenhangdong,Action act)
       {
           try
           {
               act();
               MessageBox.Show(tenhangdong+" thành công");
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

        #region Loc Dữ Liệu
       public static DbQuanLiTBDataContext dc = new DbQuanLiTBDataContext();
       public static DataTable  LocData( taikhoan tk,string text)
       {
           Reporting.TaiKhoanDataTable tb =new Reporting.TaiKhoanDataTable();
           switch (tk)
           {
               case taikhoan.MaTk:
                   var query = QLTaiKhoan.FindByCondition(x => Regex.IsMatch(x.MaTK.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.TenDangNhap, x.MatKhau, x.LanCuoiDangNhap, x.MayDangNhap, x.TrangThai));
                   break;
               case taikhoan.TenDangNhap:
                   query = QLTaiKhoan.FindByCondition(x => Regex.IsMatch(x.TenDangNhap.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.TenDangNhap, x.MatKhau, x.LanCuoiDangNhap, x.MayDangNhap, x.TrangThai));
                   break;
               case taikhoan.LanDangNhapCuoi:
                   query = QLTaiKhoan.FindByCondition(x =>x.LanCuoiDangNhap!=null && Regex.IsMatch(x.LanCuoiDangNhap.Value.ToString(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.TenDangNhap, x.MatKhau, x.LanCuoiDangNhap, x.MayDangNhap, x.TrangThai));
                   break;
               case taikhoan.MayDangNhap:
                   query = QLTaiKhoan.FindByCondition(x => Regex.IsMatch(x.MayDangNhap.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.TenDangNhap, x.MatKhau, x.LanCuoiDangNhap, x.MayDangNhap, x.TrangThai));
                   break;
               case taikhoan.TrangThai:
                   query = QLTaiKhoan.FindByCondition(x => Regex.IsMatch(x.TrangThai.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.TenDangNhap, x.MatKhau, x.LanCuoiDangNhap, x.MayDangNhap, x.TrangThai));
                   break;
               default:
                   break;
           }
           return (DataTable)tb;
       }

       public static DataTable LocData(thietbi ktb, string text)
       {
           Reporting.ThietBiDataTable tb = new Reporting.ThietBiDataTable();
           switch (ktb)
           {
               case thietbi.MaTB:
                   var query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.LoaiTB:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.LoaiTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.NhanHieu:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.NhanHieu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.XuatXu:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.XuatXu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.NgayNhap:
                   query = QLThietBi.FindByCondition(x =>x.NgayNhap!=null && Regex.IsMatch(x.NgayNhap.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.NgayLapRap:
                    query = QLThietBi.FindByCondition(x =>x.NgayLapRap!=null && Regex.IsMatch(x.NgayLapRap.ToString().ToLower(), text.ToLower()));
                    query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.TinhTrang:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.TinhTrang.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               default:
                   break;
           }
           return (DataTable)tb;
       }

       public static DataTable LocData(thongtintaikhoan tttk,string text)
       {
           Reporting.ThongTinTaiKhoanDataTable tb = new Reporting.ThongTinTaiKhoanDataTable();
           switch (tttk)
           {
               case thongtintaikhoan.MaTK:
                   var query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.MaTK.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.Ho:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.Ho.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.Ten:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.Ten.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.NgaySinh:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => x.NgaySinh !=null && Regex.IsMatch(x.NgaySinh.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.CMND:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.CMND.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.SDT:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.SDT.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               case thongtintaikhoan.ChucVu:
                   query = Generic.FindByCondition<ThongTinNguoiDung>(x => Regex.IsMatch(x.ChucVu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTK, x.Ho, x.Ten, x.NgaySinh, x.CMND, x.SDT, x.ChucVu));
                   break;
               default:
                   break;
           }

           return (DataTable)tb;
       }

       public static DataTable LocData(thongbaoloimt tbmt,string text)
       {
           Reporting.TBLoiMTDataTable tb = new Reporting.TBLoiMTDataTable();
           switch (tbmt)
           {
               case thongbaoloimt.MaLoiMT:
                   var query = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.MaLoiMT.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiMT, x.MaLoiTB, x.TenPhanBiLoi, x.ChiTietLoi));
                   break;
               case thongbaoloimt.MaLoiTB:
                   query = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.MaLoiTB.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiMT, x.MaLoiTB, x.TenPhanBiLoi, x.ChiTietLoi));
                   break;
               case thongbaoloimt.TenPhanBiLoi:
                   query = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.TenPhanBiLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiMT, x.MaLoiTB, x.TenPhanBiLoi, x.ChiTietLoi));
                   break;
               case thongbaoloimt.ChiTietLoi:
                   query = QLTBLoiMayTinh.FindByCondition(x => Regex.IsMatch(x.ChiTietLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiMT, x.MaLoiTB, x.TenPhanBiLoi, x.ChiTietLoi));
                   break;
               default:
                   break;
           }
           return (DataTable)tb;
       }

       public static DataTable LocData(thongbaoloitb tbltb,string text)
       {
           Reporting.TBLoiTBDataTable tb = new Reporting.TBLoiTBDataTable();
           switch (tbltb)
           {
               case thongbaoloitb.MaLoiTB:
                   var query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaLoiTB.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.MaNguoiGui:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaNguoiGui.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.MaTB:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.LoaiTB:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.LoaiTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.NoiDungLoi:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.NoiDungLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.NgayGui:
                   query = QLTBLoiThietBi.FindByCondition(x => x.NgayGui!=null && Regex.IsMatch(x.NgayGui.Value.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.GhiChu:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.GhiChu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               default:
                   break;
           }
           return tb;
       }

       public static DataTable LocData(dexuat dx,string text)
       {
           Reporting.DeXuatDataTable tb = new Reporting.DeXuatDataTable();
           switch (dx)
           {
               case dexuat.MaDX:
                   var query = Generic.FindByCondition<DeXuat>(x => Regex.IsMatch(x.MaDX.ToString().ToLower(), text.ToLower())); 
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaDX, x.MaNguoiGui, x.NoiDung));
                   break;
               case dexuat.MaNguoiGui:
                   query = Generic.FindByCondition<DeXuat>(x => Regex.IsMatch(x.MaNguoiGui.ToLower(), text.ToLower())); 
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaDX, x.MaNguoiGui, x.NoiDung));
                   break;
               case dexuat.NoiDung:
                   query = Generic.FindByCondition<DeXuat>(x => Regex.IsMatch(x.NoiDung.ToLower(), text.ToLower())); 
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaDX, x.MaNguoiGui, x.NoiDung));
                   break;
               default:
                   break;
           }
           return tb;
       }

       public static DataTable LocData(ketquabaotri kq,string text)
       {
           Reporting.KetQuaBaoTriDataTable tb = new Reporting.KetQuaBaoTriDataTable();
           switch (kq)
           {
               case ketquabaotri.MaThongBao:
                   var query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaThongBao.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               case ketquabaotri.MaNguoiGui:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaNguoiGui.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               case ketquabaotri.MaTB:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               case ketquabaotri.TinhTrangLoi:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.TinhTrangLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               case ketquabaotri.NgayBaoTri:
                   query = QLKQBaoTriTB.FindByCondition(x => x.NgayBaoTri!=null && Regex.IsMatch(x.NgayBaoTri.Value.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               case ketquabaotri.KetQua:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.KetQua.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x=>tb.Rows.Add(x.MaThongBao,x.MaNguoiGui,x.MaTB,x.TinhTrangLoi,x.NgayBaoTri,x.KetQua));
                   break;
               default:
                   break;
           }
           return tb;
       }
        #endregion

        #region Thống Kê Dữ Liệu
       public static DataTable ThongKe(thietbi tbtk, string text, int tuthang, int denthang)
       {
           DataTable tb = new Reporting.ThietBiDataTable();
           switch (tbtk)
           {
               case thietbi.MaTB:
                   var query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.LoaiTB:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.LoaiTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.NhanHieu:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.NhanHieu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.XuatXu:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.XuatXu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               case thietbi.NgayNhap:
                   if (char.IsNumber(text, text.Length - 1))
                   {
                       if (tuthang > 0 && denthang > 0)
                       {
                           query = QLThietBi.GetAll().Where(x => x.NgayNhap.Year == int.Parse(text) && (x.NgayNhap.Month >= tuthang && x.NgayNhap.Month <= denthang));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                       }
                       else
                       {
                           query = QLThietBi.GetAll().Where(x => x.NgayNhap.Year==int.Parse(text));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                       }
                   }
                   break;
               case thietbi.NgayLapRap:
                   if (char.IsNumber(text, text.Length - 1))
                   {
                       if (tuthang > 0 && denthang > 0)
                       {
                           query = QLThietBi.GetAll().Where(x => x.NgayLapRap != null && (x.NgayLapRap.Value.Year == int.Parse(text) && x.NgayLapRap.Value.Month >= tuthang && x.NgayLapRap.Value.Month <= denthang));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                       }
                       else
                       {
                           query = QLThietBi.GetAll().Where(x => x.NgayLapRap != null && x.NgayLapRap.Value.Year == int.Parse(text));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                       }
                  }
                   break;
               case thietbi.TinhTrang:
                   query = QLThietBi.FindByCondition(x => Regex.IsMatch(x.TinhTrang.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaTB, x.LoaiTB, x.NhanHieu, x.XuatXu, x.NgayNhap, x.NgayLapRap, x.TinhTrang));
                   break;
               default:
                   break;
           }
           return tb;
       }

       public static DataTable ThongKe(thongbaoloitb tbltb, string text, int tuthang, int denthang)
       {
           Reporting.TBLoiTBDataTable tb = new Reporting.TBLoiTBDataTable();
           switch (tbltb)
           {
               case thongbaoloitb.MaLoiTB:
                   var query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaLoiTB.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.MaNguoiGui:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaNguoiGui.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.MaTB:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.LoaiTB:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.LoaiTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.NoiDungLoi:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.NoiDungLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               case thongbaoloitb.NgayGui:
                   if (char.IsNumber(text, text.Length - 1))
                   {
                       if (tuthang > 0 && denthang > 0)
                       {
                           query = QLTBLoiThietBi.GetAll().Where(x => x.NgayGui != null && (x.NgayGui.Value.Year == int.Parse(text) && x.NgayGui.Value.Month >= tuthang && x.NgayGui.Value.Month <= denthang));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                       }
                       else
                       {
                           query = QLTBLoiThietBi.GetAll().Where(x => x.NgayGui != null && x.NgayGui.Value.Year == int.Parse(text));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                       }
                   }
                   break;
               case thongbaoloitb.GhiChu:
                   query = QLTBLoiThietBi.FindByCondition(x => Regex.IsMatch(x.GhiChu.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaLoiTB, x.MaNguoiGui, x.MaTB, x.LoaiTB, x.NoiDungLoi, x.NgayGui, x.GhiChu));
                   break;
               default:
                   break;
           }
           return tb;
       }

       public static DataTable ThongKe(ketquabaotri kq, string text, int tuthang, int denthang)
       {
           Reporting.KetQuaBaoTriDataTable tb = new Reporting.KetQuaBaoTriDataTable();
           switch (kq)
           {
               case ketquabaotri.MaThongBao:
                   var query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaThongBao.ToString().ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                   break;
               case ketquabaotri.MaNguoiGui:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaNguoiGui.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                   break;
               case ketquabaotri.MaTB:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.MaTB.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                   break;
               case ketquabaotri.TinhTrangLoi:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.TinhTrangLoi.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                   break;
               case ketquabaotri.NgayBaoTri:
                   if (char.IsNumber(text, text.Length - 1))
                   {
                       if (tuthang > 0 && denthang > 0)
                       {
                           query = QLKQBaoTriTB.GetAll().Where(x => x.NgayBaoTri != null && (x.NgayBaoTri.Value.Year == int.Parse(text) && x.NgayBaoTri.Value.Month >= tuthang && x.NgayBaoTri.Value.Month <= denthang));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                       }
                       else
                       {
                           query = QLKQBaoTriTB.GetAll().Where(x => x.NgayBaoTri != null && x.NgayBaoTri.Value.Year == int.Parse(text));
                           query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                       }
                   }
                   break;
               case ketquabaotri.KetQua:
                   query = QLKQBaoTriTB.FindByCondition(x => Regex.IsMatch(x.KetQua.ToLower(), text.ToLower()));
                   query.ToList().ForEach(x => tb.Rows.Add(x.MaThongBao, x.MaNguoiGui, x.MaTB, x.TinhTrangLoi, x.NgayBaoTri, x.KetQua));
                   break;
               default:
                   break;
           }
           return tb;
       }
        #endregion
    }
}
